import axios from "./axios"
import {header1,header2} from './common'
import {order_list,order_search_id, order_update,} from './../api/order'

export function getorderList(){//得到数据列表
  return  axios({
    method:'get',
    url:order_list,
    headers:{
      ...header1
    }
  })
}
export function getorder(objectId: string) {
  if (!objectId) {
    console.error('订单ID为空，无法发起请求');
    return Promise.reject(new Error('订单ID为空'));
  }
  return axios({
    method: 'get',
    url: order_search_id + objectId, // order_search_id = "/1.1/classes/order/"
    headers: { ...header1 }
  })
}
export function updateorder(id:number, planId:string, name:string, objectId:string, data?: any) {
  return axios({
    method: 'put',
    url: order_update + objectId,
    headers: {
      ...header2,
      'Content-Type': 'application/json'
    },
    data: data || {
      id: id,
      planId: planId,
      name: name
    }
  })
}

function orderList() {
  getorderList().then((response) => {
    console.log('API响应:', response);
    if (response.data && response.data.results) {
      orders.value = response.data.results.map(item => ({
        objectId: item.objectId,
        order_id: item.order_id,
        courseName: item.courseName || '',
        price: item.price ? String(item.price).trim() : '0',
        user_name: item.user_name || '',
        status: item.status ? String(item.status).trim() : '',
        pay_method: item.pay_method && item.pay_method.trim() ? item.pay_method.trim() : '未支付',
        notes: item.notes || '',
        order_time: item.order_time || item.createdAt || item.updatedAt || ''
      }))
      console.log('转换后订单数据:', orders.value)
    }
  }).catch((err) => {
    ElMessage.error('获取订单列表失败')
  })
}